// function includeExJS(jsFilepath){
//     var exJS = document.createElement('script');
//     exJS.src = jsFilepath;
//     document.head.appendChild(exJS);
// }
// includeExJS("/base.js");

$(function () {
	const baseList = [
		{
			image: './img/baseInfo.png',
			title: '基本信息',
			desc: '建档必需的个人基本信息',
			linkUrl: './information/baseInfo.html',
			hasFinish: true,
		},
		{
			image: './img/detailInfo.png',
			title: '详细信息（选填）',
			desc: '您的糖尿病基本数据，可联系医助帮填',
			linkUrl: './information/detailInfo.html',
			hasFinish: true,
		},
		{
			image: './img/lifeQuestion.png',
			title: '生活调研问卷',
			desc: '有助于医生更全面地了解您的病情与生活状况',
			linkUrl: './information/lifeIndex.html',
			hasFinish: true,
		},
		{
			image: './img/reverse.png',
			title: '缓解调查问卷',
			desc: '帮助医生判断您的缓解情况',
			linkUrl: './information/reverseIndex.html',
			hasFinish: true,
		},
		{
			image: './img/report.png',
			title: '报告单',
			desc: '既往病史、实验室检测、影像学检测等相关报告单',
			linkUrl: './information/report.html',
			hasFinish: false,
		},
		// {
		// 	image: './img/evalution.png',
		// 	title: '量表评估',
		// 	desc: '利用专业量表辅助评估您的病情及身心状态',
		// 	linkUrl: '',
		// 	hasFinish: false,
		// },
		// {
		// 	image: './img/riskJudge.png',
		// 	title: '并发症风险判断',
		// 	desc: '帮助您判断自身存在的糖尿病并发症风险',
		// 	linkUrl: '',
		// },
	]
	//循环添加tab
	$.each(baseList, function (i, v) {
		var html = `
        <li linkpath="${v.linkUrl}">
            <img class="base-img" src=${v.image} />
            <div class="base-content">
                <div class="base-content-top">
                    <span>${v.title}</span>
                    ${v.hasFinish ? '<span class="status-ico"></span>' : ''}
                </div>
                <div class="base-content-bottom">
                    <span>${v.desc}</span>
                </div>
            </div>
            <img class="base-list-arrow" src="./img/arrow.png"></img>
        </li>`
		$(html).appendTo('.index-list ul')
	})

	const btn = `
							<div style="position:relative">
								<li style="border-top: 1px solid #dddddd;">
									<img class="base-img" src="./img/evalution.png" />
									<div class="base-content">
											<div class="base-content-top">
													<span>量表评估</span>
											</div>
											<div class="base-content-bottom">
													<span>利用专业量表辅助评估您的病情及身心状态</span>
											</div>
									</div>
									<img class="base-list-arrow" src="./img/arrow.png"></img>
								</li>
								<div id="launch-btn" ></div>
							</div>
							`
	$(btn).appendTo('.index-list ul')

	//循环添加tab点击跳转
	$('.index-list ul li').each(function (i) {
		$('.index-list ul li')
			.eq(i)
			.click(function () {
				if ($(this).attr('linkpath')) {
					window.location.href = $(this).attr('linkpath')
				} else {
					syalert.syopen('alert3')
				}
			})
	})
	$('.ins-top-phone').click(function () {
		window.location.href = 'tel://4006677079'
	})
	const patientId = localStorage.getItem('pid')
	$.ajax({
		type: 'GET',
		url:
			window.location.host.search('dps') != -1
				? '/dps-jeecg-boot'
				: '/jeecg-boot' + '/archive/index?patientId=' + patientId,
		dataType: 'json',
		contentType: 'application/json',
		success: function (response) {
			if (response.success) {
				var img = $('.status-ico')
				const res = response.result
				console.log(res)
				if (res.basicInfoIsFinished) {
					img.eq(0).css('background-image', "url('./img/finish.png')")
				}
				if (res.detailInfoIsFinished) {
					img.eq(1).css('background-image', "url('./img/finish.png')")
				}
				if (res.lifewayResearchIsFinished) {
					img.eq(2).css('background-image', "url('./img/finish.png')")
					$('.index-list ul li')
						.eq(2)
						.click(function () {
							window.location.href =
								'./information/lifeEnd.html?id=' + patientId
						})
				}
				if (res.reversionResearchIsFinished) {
					img.eq(3).css('background-image', "url('./img/finish.png')")
				}
				if (res.complicationResearchIsFinished) {
					img.eq(4).css('background-image', "url('./img/finish.png')")
				}
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {},
	})

	function is_weixn() {
		let ua = navigator.userAgent.toLowerCase()
		console.log(navigator.userAgent.toLowerCase())
		if (ua.match(/MicroMessenger/i) && !ua.match(/wxwork/i)) {
			return true
		} else {
			return false
		}
	}

	function postAction(url, parameter) {
		if (!parameter) {
			parameter = {}
		}
		return axios({
			url: url,
			method: 'post',
			data: parameter,
		})
	}

	function wechatInit() {
		if (!is_weixn()) {
			return
		}

		postAction(
			window.location.host.search('dps') != -1
				? '/dps-jeecg-boot'
				: '/jeecg-boot' + '/sys/wechat/signature',
			{
				pageUrl: window.location.href,
			}
		).then(res => {
			res = res.data
			console.log('openWxmini', res)
			if (res.success) {
				let result = res.result
				wx.config({
					debug: false, // 调试时可开启
					appId: result.appId, // <!-- replace -->
					timestamp: result.timestamp, // 必填，填任意数字即可
					nonceStr: result.nonceStr, // 必填，填任意非空字符串即可
					signature: result.signature, // 必填，填任意非空字符串即可
					jsApiList: [
						'chooseImage',
						'previewImage',
						'checkJsApi', //判断当前客户端版本是否支持指定JS接口
					], // 必填，随意一个接口即可
					openTagList: ['wx-open-launch-weapp'], // 填入打开小程序的开放标签名
				})
			}
		})
	}

	function wx_launch(info) {
		if (!is_weixn()) {
			return
		} else {
			console.log('ok')
			var btn = document.getElementById(info.eleId) //获取元素
			let script = document.createElement('script') // 创建script内容插槽 避免template标签冲突
			script.type = 'text/wxtag-template' // 使用script插槽 必须定义这个type
			script.text = info.content // 自定义的html字符串内容
			console.log(script.outerHTML)
			let html = `<wx-open-launch-weapp style="position:absolute;top:0;left:0;width:100%;height: 1.46rem;" username="${info.appid}" path="${info.url}">${script.outerHTML}</wx-open-launch-weapp>`
			btn.innerHTML = html // html字符串赋值

			// 点击按钮 正常跳转触发
			btn.addEventListener('launch', function (e) {
				console.log('success')
			})
			// 点击跳转 抛出异常
			btn.addEventListener('error', function (e) {
				console.log('fail', e.detail)
				alert(`跳转异常 - ${e.detail}`)
			})
		}
	}
	wechatInit()
	const content1 = `
        <div style="position:absolute;top:0;left:0;width:100%;height:100%;opacity:0"></div>
		`
	let launchParams = {
		eleId: 'launch-btn', // 元素id
		url: 'pages/evalutionIndex/evalutionIndex.html?pid=' + localStorage.pid, // 跳转小程序的页面路径
		content: content1, // 自定义的html内容
		appid: 'gh_f8c6c4b4cd8c',
	}
	wx_launch(launchParams)
})
